package com.yourbatis.interpreter;

import com.yourbatis.lexical.Lexer;
import com.yourbatis.syntax.ASTNode;
import com.yourbatis.syntax.Statement;
import com.yourbatis.syntax.Syntax;

import java.util.Map;

public class Interpreter {
    public static Object interpret(String input)
    {
        return interpret(input, null);
    }

    public static Object interpret(String input, Map map)
    {
        Lexer lexer = new Lexer(input);
        Syntax syntax = new Syntax(lexer.tokenize());
        syntax.parser();
        Environment.init(map);
        Object val = null;
        for (ASTNode node : syntax.getNodeList()) {
            if (node instanceof Statement) {
                val = ((Statement) node).execute();
            }else{
                val = node.eval();
            }
        }
        return val;
    }
}
